# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Xin Sun <sun.simpson@gmail.com>

_realname=hdf5
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_ver=1.8.16
patch=
pkgver=${_ver}${patch//-/.}
pkgrel=2
pkgdesc="General purpose library and file format for storing scientific data"
arch=('any')
url="https://www.hdfgroup.org/HDF5/"
source=("https://www.hdfgroup.org/ftp/HDF5/releases/hdf5-${_ver}${patch}/src/hdf5-${_ver}${patch}.tar.bz2"
        "hdf5-link-ws2_32-mingw.patch"
        "hdf5-default-import-suffix.patch"
        "hdf5-install.patch"
        "hdf5-fix-install-docs.patch"
        "hdf5-proper-library-names-mingw.patch"
        "hdf5-fix-find-szip.patch"
        )
sha256sums=('13aaae5ba10b70749ee1718816a4b4bfead897c2fcb72c24176e759aec4598c6'
            'e52b64a4183046364ca2a80523b242071f8c17ec7b6ea1956dc7a70108f71179'
            '33504d93b55c267adea3d05f07467a088de5b460e100c7e364be2a05ac5502e3'
            'b1a38b5ef1d77a577bba501622bb6fcef5bec1de2828e8ccc6e298d4d4d9239d'
            '72a4c3ff11b0811e2462252ddc7f14428c0241c71b3403408dbc0f93a21271c9'
            'c2a00ca231b15011c04d161fc1caac3d99554847ccdf46d82ac569beee11b342'
            '46ccb300164784efac6415f21afdad9d84444abd46cf3e896c3ad00ac7d6ed63')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran"
         "${MINGW_PACKAGE_PREFIX}-szip"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-gcc-fortran"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "patch")
options=('staticlibs' 'strip')

prepare() {
  cd ${srcdir}/${_realname}-${_ver}${patch}
  patch -p1 -i ${srcdir}/hdf5-link-ws2_32-mingw.patch
  patch -p1 -i ${srcdir}/hdf5-install.patch
  patch -p1 -i ${srcdir}/hdf5-default-import-suffix.patch
  patch -p1 -i ${srcdir}/hdf5-fix-install-docs.patch
  patch -p1 -i ${srcdir}/hdf5-proper-library-names-mingw.patch
  patch -p1 -i ${srcdir}/hdf5-fix-find-szip.patch

  # rename X.dll to X-0.dll
  # echo "IF (BUILD_SHARED_LIBS)" >> CMakeLists.txt
  # echo "SET_TARGET_PROPERTIES (\${HDF5_LIB_TARGET} PROPERTIES SUFFIX -0.dll)">> CMakeLists.txt
  # echo "SET_TARGET_PROPERTIES (\${HDF5_HL_LIB_TARGET} PROPERTIES SUFFIX -0.dll)">> CMakeLists.txt
  # echo "SET_TARGET_PROPERTIES (\${HDF5_CPP_LIB_TARGET} PROPERTIES SUFFIX -0.dll)">> CMakeLists.txt
  # echo "SET_TARGET_PROPERTIES (\${HDF5_HL_CPP_LIB_TARGET} PROPERTIES SUFFIX -0.dll)">> CMakeLists.txt
  # echo "SET_TARGET_PROPERTIES (\${HDF5_TOOLS_LIB_TARGET} PROPERTIES SUFFIX -0.dll)">> CMakeLists.txt
  # echo "SET_TARGET_PROPERTIES (\${HDF5_F90_LIB_TARGET} PROPERTIES SUFFIX -0.dll)">> CMakeLists.txt
  # echo "SET_TARGET_PROPERTIES (\${HDF5_F90_C_LIB_TARGET} PROPERTIES SUFFIX -0.dll)">> CMakeLists.txt
  # echo "SET_TARGET_PROPERTIES (\${HDF5_HL_F90_LIB_TARGET} PROPERTIES SUFFIX -0.dll)">> CMakeLists.txt
  # echo "SET_TARGET_PROPERTIES (\${HDF5_HL_F90_C_LIB_TARGET} PROPERTIES SUFFIX -0.dll)">> CMakeLists.txt
  # echo "ELSE ()" >> CMakeLists.txt
  # echo "SET_TARGET_PROPERTIES (\${HDF5_LIB_TARGET} PROPERTIES PREFIX \"\")">> CMakeLists.txt
  # echo "SET_TARGET_PROPERTIES (\${HDF5_HL_LIB_TARGET} PROPERTIES PREFIX \"\")">> CMakeLists.txt
  # echo "SET_TARGET_PROPERTIES (\${HDF5_CPP_LIB_TARGET} PROPERTIES PREFIX \"\")">> CMakeLists.txt
  # echo "SET_TARGET_PROPERTIES (\${HDF5_HL_CPP_LIB_TARGET} PROPERTIES PREFIX \"\")">> CMakeLists.txt
  # echo "SET_TARGET_PROPERTIES (\${HDF5_TOOLS_LIB_TARGET} PROPERTIES PREFIX \"\")">> CMakeLists.txt
  # echo "SET_TARGET_PROPERTIES (\${HDF5_F90_LIB_TARGET} PROPERTIES PREFIX \"\")">> CMakeLists.txt
  # echo "SET_TARGET_PROPERTIES (\${HDF5_F90_C_LIB_TARGET} PROPERTIES PREFIX \"\")">> CMakeLists.txt
  # echo "SET_TARGET_PROPERTIES (\${HDF5_HL_F90_LIB_TARGET} PROPERTIES PREFIX \"\")">> CMakeLists.txt
  # echo "SET_TARGET_PROPERTIES (\${HDF5_HL_F90_C_LIB_TARGET} PROPERTIES PREFIX \"\")">> CMakeLists.txt
  # echo "ENDIF ()" >> CMakeLists.txt
}

build() {
  # Build shared library
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -Wno-dev \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED_LIBS=ON \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_SKIP_RPATH=ON \
    -DHDF5_BUILD_HL_LIB=ON \
    -DHDF5_BUILD_CPP_LIB=ON \
    -DHDF5_BUILD_FORTRAN=ON \
    -DHDF5_BUILD_TOOLS=ON \
    -DHDF5_ENABLE_DEPRECATED_SYMBOLS=ON \
    -DHDF5_ENABLE_SZIP_SUPPORT=ON \
    -DHDF5_ENABLE_Z_LIB_SUPPORT=ON \
    ../${_realname}-${_ver}${patch}

  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install
}
